package mail.claux.com;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.Preference;
import android.preference.PreferenceManager;

public class MailSettings extends PreferenceActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.mailsettings);
		
		final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("view_pwd");
		//listener associato al check box
		checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				//ricavo la password
		        String passwordPref = prefs.getString("password", "");
		        if(passwordPref.equalsIgnoreCase("")){
		        	checkboxPref.setSummary("Your password: (empty)");
		        }
		        else if (newValue.toString().equals("true")){
			        //la visualizzo nel summary del checkbox
			        checkboxPref.setSummary("Your password: "+passwordPref+"");
			    }
			    else{
			        checkboxPref.setSummary("Your password: ********");
			    }
			    return true;
			}
		});
		
		final EditTextPreference editPwdPref = (EditTextPreference) getPreferenceManager().findPreference("password");
		//listener associato all'edit text della password
		editPwdPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				//ricavo il valore della checkbox
		        boolean checkBoxPref = prefs.getBoolean("view_pwd", false);
		        if(newValue.toString().equalsIgnoreCase("")){
			       	checkboxPref.setSummary("Your password: (empty)");
			    }
			    else if (checkBoxPref){	//true
				    //la visualizzo nel summary del checkbox
				    checkboxPref.setSummary("Your password: "+newValue.toString()+"");
				}
				else{	//false
				    checkboxPref.setSummary("Your password: ********");
				}
		        return true;
			}
		});
	}
}
